package com.tico.translatereditor.controller;

import com.alibaba.fastjson.JSONObject;
import com.tico.common.pojo.Result;
import com.tico.common.pojo.ResultHelper;
import com.tico.common.utils.BaseQuery;
import com.tico.common.utils.JsonUtils;
import com.tico.common.utils.QueryResult;
import com.tico.translatereditor.domain.Kafka;
import com.tico.translatereditor.domain.TaskLinsten;
import com.tico.translatereditor.param.KafkaParam;
import com.tico.translatereditor.param.TaskLinstenParam;
import com.tico.translatereditor.service.KafkaService;
import com.tico.translatereditor.service.TaskLinstenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
@RequestMapping("/task")
@Slf4j
public class TaskLinstenController {
    @Autowired
    private TaskLinstenService taskLinstenService;

    @PostMapping(value = "/save")
    public JSONObject test(@RequestBody TaskLinsten taskLinsten, HttpServletRequest request) {
        Result res = new Result();
        try {
            taskLinsten.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            taskLinstenService.insert(taskLinsten);
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }
    @GetMapping(value = "/list")
    public JSONObject list(TaskLinstenParam linstenParam) {
        Result result = new Result();
        try {
            BaseQuery baseQuery = new BaseQuery();
            baseQuery.setPage(linstenParam.getPage());
            baseQuery.setLimit(linstenParam.getLimit());
            QueryResult<TaskLinsten> pageInfo = taskLinstenService.findPageInfo(baseQuery);
            result.addModel("count",pageInfo.getTotalRecord());
            result.addModel("data", JsonUtils.toUnderlineJSONFilterString(TaskLinsten.class,pageInfo.getResultList(),new String[]{}) );
            result.setMessage("成功");
            result.setSuccess(true);
        }catch (Exception e){
            result.setMessage("失败");
            result.setSuccess(false);
        }
        return ResultHelper.renderAsJsonWipeData(result);
    }
    @PostMapping(value = "/delete")
    public JSONObject delete(@RequestBody TaskLinstenParam linstenParam, HttpServletRequest request) {
        Result res = new Result();
        try {
            String str[] = linstenParam.getIds().split(",");
            for(int i=0;i<str.length;i++){
                taskLinstenService.delete(str[i]);
            }
            res.setMessage("成功");
            res.setSuccess(true);
        }catch (Exception e){
            res.setMessage("失败");
            res.setSuccess(false);
        }
        return ResultHelper.renderAsJson(res);
    }

//    @PostMapping(value = "/startUsing")
//    public JSONObject startUsing(@RequestBody Kafka kafka, HttpServletRequest request) {
//        Result res = new Result();
//        try {
//            String str[] = kafka.getIds().split(",");
//            for(int i=0;i<str.length;i++){
//                kafka.setId(str[i]);
//                kafkaService.update(kafka);
//            }
//            res.setMessage("成功");
//            res.setSuccess(true);
//        }catch (Exception e){
//            res.setMessage("失败");
//            res.setSuccess(false);
//        }
//        return ResultHelper.renderAsJson(res);
//    }
}
